package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Fragment;
import cz.anime.ppro.model.Webuser;

@Component
@ManagedBean
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class UserAdministrationBean {

	List<Webuser> users;
	
	@Autowired
	private UserComponent userComponent;

	@PostConstruct
	private void init(){
		users = userComponent.getUsers();
	}
	
	public void deleteUser(int userId){
		Webuser user = userComponent.getUser(userId);
		
		userComponent.delete(user);
		getUsers().remove(user);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/admin-users.jsf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public List<Webuser> getUsers() {
		return users;
	}

	public void setUsers(List<Webuser> users) {
		this.users = users;
	}
	
	
	
}
